package com.servicesystem.servicesystem.controller.ali;

import com.servicesystem.servicesystem.entity.AliOrderLogistics;
import com.servicesystem.servicesystem.service.AliOrderLogisticsService;
import com.servicesystem.servicesystem.service.ali.AliOrderService;
import com.servicesystem.servicesystem.untis.HttpRequestAll;
import com.servicesystem.servicesystem.untis.R;
import com.servicesystem.servicesystem.untis.ali.AliData;
import com.servicesystem.servicesystem.untis.ali.AliSign;
import com.servicesystem.servicesystem.untis.ali.RequestUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ali-order/1688")
public class AliOrderController {

    private final static HttpRequestAll httpRequestAll = new HttpRequestAll();
    @Autowired
    private AliOrderService aliOrderService;
    @Autowired
    private AliOrderLogisticsService aliOrderLogisticsService;

    /**
     * 取消订单 - 1688
     */
    @DeleteMapping("cancel-order/{orderId}")
    public R cancelOrder(@PathVariable(value = "orderId") String orderId) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("access_token", AliData.ACCESS_TOKEN);
            params.put("webSite", "1688");
            params.put("tradeID", orderId);
            params.put("cancelReason", "other");
            String signature = AliSign.getSignature("param2/1/com.alibaba.trade/alibaba.trade.cancel/5307039", params);
            Map<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("access_token", AliData.ACCESS_TOKEN);
            objectObjectHashMap.put("webSite", "1688");
            objectObjectHashMap.put("tradeID", orderId);
            objectObjectHashMap.put("cancelReason", "other");
            objectObjectHashMap.put("_aop_signature", signature);
            // 删除数据库
            R r = httpRequestAll.doPostWithParams(RequestUrl.CANCEL_ORDER, objectObjectHashMap);
            aliOrderLogisticsService.removeById(orderId);
            return r;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("取消失败");
    }

    /**
     * 获取订单详情 含物流信息 - 1688
     */
    @PostMapping("get-order-detail/{orderId}")
    public R getOrderDetail(@PathVariable(value = "orderId") String orderId) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("access_token", AliData.ACCESS_TOKEN);
            params.put("webSite", "1688");
            params.put("orderId", orderId);
            String signature = AliSign.getSignature("param2/1/com.alibaba.trade/alibaba.trade.get.buyerView/5307039", params);
            Map<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("access_token", AliData.ACCESS_TOKEN);
            objectObjectHashMap.put("webSite", "1688");
            objectObjectHashMap.put("orderId", orderId);
            objectObjectHashMap.put("_aop_signature", signature);
            return httpRequestAll.doPostWithParams(RequestUrl.GET_ALI_ORDER_DETAIL, objectObjectHashMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("获取失败");
    }

    /**
     * 获取物流信息
     */
    @PostMapping("get-logisticsInfo/{orderId}")
    public R getLogisticsInfo(@PathVariable(value = "orderId") String orderId) {
        return aliOrderService.getLogisticsInfo(orderId);
    }

    /**
     * 获取物流跟踪信息
     */
    @PostMapping("get-logisticsInfo-stream/{orderId}")
    public R getLogisticsInfoStream(@PathVariable(value = "orderId") String orderId) {
        return aliOrderService.getLogisticsInfoStream(orderId);
    }

    @PostMapping("check-library-total")
    public R checkLibraryTotal(String cargoParamList, String addressParam) {

        return aliOrderService.checkLibraryTotal(cargoParamList, addressParam);

    }
}
